#include "FreeRTOS.h"
#include "task.h"

extern "C"
{
	extern int __stack_limit;

#define HEAP_BEGIN ((uint32_t)(&__stack_limit))
#define HEAP_END ((uint32_t)(0x20000000+0x18000))

	volatile uint32_t AllRamBytes;
	HeapRegion_t xHeapRegions[2];
	static int IsHeapInit = 0;
	void HeapInit()
	{
		if (IsHeapInit != 0)
			return;
		IsHeapInit = 1;
		xHeapRegions[0].pucStartAddress = (uint8_t *)HEAP_BEGIN;
		xHeapRegions[0].xSizeInBytes = HEAP_END - HEAP_BEGIN;

		xHeapRegions[1].pucStartAddress = (uint8_t *)NULL;
		xHeapRegions[1].xSizeInBytes = 0;

		AllRamBytes = xHeapRegions[0].xSizeInBytes;
		vPortDefineHeapRegions(xHeapRegions);
	}
}
void *operator new(size_t size)
{
	HeapInit();
	void *p = pvPortMalloc(size);
	configASSERT(p != nullptr);
	return p;
}

void *operator new[](size_t size)
{
	HeapInit();
	void *p = pvPortMalloc(size);
	configASSERT(p != nullptr);
	return p;
}

void operator delete(void *ptr)
{
	vPortFree(ptr);
}

void operator delete[](void *ptr)
{
	vPortFree(ptr);
}
extern "C" void *malloc(size_t size)
{
	HeapInit();	
	void *p = pvPortMalloc(size);
	configASSERT(p != nullptr);
	return p;
}
extern "C" void free(void *ptr)
{
	vPortFree(ptr);
}

void __cxa_pure_virtual(void)
{
	configASSERT(0);
}